package com.cai.oamanager.controller;

import com.cai.oamanager.entity.Leave;
import com.cai.oamanager.entity.PageUtil;
import com.cai.oamanager.entity.People;
import com.cai.oamanager.entity.Student;
import com.cai.oamanager.service.LeaveSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/3/2 16 : 54
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    private LeaveSevice leaveSevice;

    /**
     * 学生获取自己的历史请假记录和状态
     * @param model
     * @param request
     * @param currentPage
     * @return
     */
    @RequestMapping("/getAllLeave.html")
    public String getAllLeave(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer currentPage){
        PageUtil pageUtil = new PageUtil();
        Student student = (Student) request.getSession().getAttribute("student");
        int leaveCount = leaveSevice.getLeaveCount(student.getSid());
        pageUtil.setSize(5);
        pageUtil.setCurrentPage(currentPage);
        pageUtil.setTotal(leaveCount);
        pageUtil.startPage();
        pageUtil.totalNumPage();
        List<Leave> allLeaves = leaveSevice.getAllLeave(student.getSid(), pageUtil.getStart(), pageUtil.getSize());
        model.addAttribute("leaveList",allLeaves);
        model.addAttribute("pageUtil",pageUtil);
        return "leave";
    }

    /**
     * 添加请假申请
     * @param leave
     * @return
     */
    @RequestMapping("/addLeave")
    public String addLeave(Leave leave){
        System.out.println(leave.getLetime());
        System.out.println(leave.toString());
        leave.setLestate(0);
        leaveSevice.addLeave(leave);
        return "redirect:/leave/getAllLeave.html";
    }

    /**
     * 辅导员审批
     */
    @RequestMapping("/approved")
    public String approvedByInstructor(HttpServletRequest request,Model model){
        People people = (People) request.getSession().getAttribute("people");
        List<Leave> leavesList = leaveSevice.getLeaves(people.getOid());
        model.addAttribute("leavesList",leavesList);
        return "approval";
    }

    @RequestMapping("/instructorApproved")
    public @ResponseBody String instructorApproved(@RequestParam Integer leid,@RequestParam Integer sta){
        leaveSevice.instructorApproved(leid,sta);
        return "success";
    }
}
